using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using System.Text;


namespace RoofRunner.GameComponents
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class DebugInfoComponent : DrawableGameComponent
    {
        #region Fields
        private KeyboardState _oldKS;
        float _elapsed, _frames, _framerate;
        #endregion // Fields


        #region Properties
        private RoofRunnerGame RRGame { get { return (RoofRunnerGame)this.Game; } }

        private bool IsVisible { get; set; }
        private SpriteFont Font { get; set; }

        private SpriteBatch SpriteBatch { get; set; }
        #endregion // Properties


        #region Constructors
        public DebugInfoComponent(Game game)
            : base(game)
        {
            this.IsVisible = true;
        }
        #endregion // Constructors


        #region Methods
        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            this.SpriteBatch = new SpriteBatch(this.Game.GraphicsDevice);
            this.Font = this.Game.Content.Load<SpriteFont>("Fonts\\Font1");
            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            KeyboardState newKS = Keyboard.GetState();
            if (_oldKS.IsKeyUp(Keys.F12) && newKS.IsKeyDown(Keys.F12))
                this.IsVisible = !this.IsVisible;
            _oldKS = newKS;

            base.Update(gameTime);
        }



        public override void Draw(GameTime gameTime)
        {
            CalcFrameRate(gameTime);
            if (!this.IsVisible) return;

            var pos = RRGame.Camera.Position;
            var yaw = RRGame.Camera.Yaw;
            var pitch = RRGame.Camera.Pitch;



            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("CameraPos: {0}\n", pos);
            sb.AppendFormat("CameraRot: {0} ; {1}\n", pitch, yaw);
            sb.AppendFormat("Framerate: {0}\n", _framerate);
            sb.AppendFormat("ActorCount: {0}\n", RRGame.Actors.Count);






            SpriteBatch.Begin();
            SpriteBatch.DrawString(Font, sb, new Vector2(10, 10), Color.White);
            SpriteBatch.End();

            base.Draw(gameTime);
        }



        private void CalcFrameRate(GameTime gameTime)
        {
            _elapsed += (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (_elapsed >= 1.0f)
            {
                _elapsed -= 1.0f;
                _framerate = _frames;
                _frames = 0;
            }
            else
                _frames += 1;
        }
        #endregion // Methods
    }
}
